import requests

#定义请求的url
url = 'https://www.baidu.com/'


#定义请求头信息
headers = {
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36'
}


#发起Get请求
res  =  requests.get(url=url,headers=headers)

#获取相应状态码，当响应状态码为200 时则表示请求成功
code = res.status_code
print(code)

#相应成功后把相应的内容加入文件中
if code ==200:
    with open ('text.html','w',encoding="utf-8") as fp:
       fp.write(res.text)

